package com.example.lowbird.lowbird.fragment;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.example.lowbird.lowbird.R;
import com.example.lowbird.lowbird.adapter.Hotadapter;
import com.example.lowbird.lowbird.bean.HotBean;
import com.example.lowbird.lowbird.constant.Contants;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link HotFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link HotFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HotFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private Context mContext;

    private Hotadapter adapter;
    private static int page = 1;
    private HotBean hotbean = null;
    private int totalpage=0;
    private  int pagesize=10;;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private MaterialRefreshLayout mrf_hotfragment;
    private ListView lv_hot;

    public HotFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HotFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HotFragment newInstance(String param1, String param2) {
        HotFragment fragment = new HotFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_hot, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
      mrf_hotfragment = ((MaterialRefreshLayout) view.findViewById(R.id.mrf_hotfragment));


        lv_hot = ((ListView) view.findViewById(R.id.lv_hot));
        adapter = new Hotadapter(mContext, hotbean);


        lv_hot.setAdapter(adapter);






        initgetdata(page);
        setmrtfragment();
    }

    //给下拉刷新控件初始化

    private void setmrtfragment() {
        mrf_hotfragment.setMaterialRefreshListener(
                new MaterialRefreshListener() {

                    @Override
                    public void onRefresh(final MaterialRefreshLayout materialRefreshLayout) {


                        if (hotbean==null){
                            adapter.clearhotbean();
                            page=1;
                            initgetdata(page);


                        }else {
                            adapter.notifyDataSetChanged();

                            new Thread(){
                                @Override
                                public void run() {

                                    try {
                                        sleep(500);
                                        mrf_hotfragment.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                mrf_hotfragment.finishRefresh();
                                            }
                                        });
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }.start();
                        }
                    }

                    public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                        //上拉刷新...
                        Log.e("HOT", "onRefreshLoadMore: "+page+"total"+totalpage );
                        if (page<totalpage){
                            page+=1;
                            initgetdata(page);
                        }else {

                            Log.e("HOt", "已经是最后一页啦 " );
                            mrf_hotfragment.finishRefreshLoadMore();
                        }

                    }
                });


        mrf_hotfragment.setIsOverLay(false);
        mrf_hotfragment.setWaveShow(true);
        mrf_hotfragment.setWaveColor(Color.RED);



    }

    private void initgetdata(final int page) {

        String url = Contants.API.WARES_HOT + "?curPage=" + page + "&pageSize="+pagesize;

        OkHttpUtils.get().url(url).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("HOT", "onError: " + e.toString());
                mrf_hotfragment.post(new Runnable() {
                    @Override
                    public void run() {
                        mrf_hotfragment.finishRefresh();
                        mrf_hotfragment.finishRefreshLoadMore();
                    }
                });

            }

            @Override
            public void onResponse(String response, int id) {
                String s = response.toString();


                final HotBean hotBean1 = new Gson().fromJson(s, HotBean.class);

                if (totalpage==0){

                    totalpage=hotBean1.getTotalCount()/pagesize+1;
                }
                lv_hot.post(new Runnable() {
                    @Override
                    public void run() {
                        adapter.addHotbean(hotBean1);


                    }
                });


                if (hotbean==null){
                    hotbean=hotBean1;
                    Log.e("HOT", "onResponse: "+hotBean1.getList().size());
                }else {

                    hotbean.getList().addAll(hotBean1.getList());
                }
                Log.e("HOT", "addHotbean: "+hotbean.getList().size() );



                mrf_hotfragment.finishRefresh();
                mrf_hotfragment.finishRefreshLoadMore();


            }


        }

        );

    }






}
